package com.cashoutgaming.client.modules.toolbar.button;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.sencha.gxt.widget.core.client.Component;

public class TextToolbarButton extends Component implements HasClickHandlers {

	private Element buttonWrap, textWrap;
	private ApplicationToolbarButtonStyle style;
	private boolean active;

	interface ApplicationToolbarButtonStyle extends CssResource {

		String toolbarButtonContainer();

		String toolbarButton();

		String toolbarButton2();

		String toolbarButton2Active();

		String toolbarButtonImage();

		String toolbarButtonText();

		String toolbarButtonText2();

		String toolbarButtonArrow();

		String toolbarButtonArrow2();
	}

	interface StyleResources extends ClientBundle {

		@Source("ApplicationToolbarButton.css")
		ApplicationToolbarButtonStyle css();
	}

	public TextToolbarButton() {
		setElement(DOM.createTable());
		final StyleResources resources = GWT.create(StyleResources.class);
		resources.css().ensureInjected();
		style = resources.css();

		buttonWrap = DOM.createTD();
		buttonWrap.setClassName(style.toolbarButton2());
		getElement().appendChild(buttonWrap);

		textWrap = DOM.createDiv();
		textWrap.setClassName(style.toolbarButtonText2());
		buttonWrap.appendChild(textWrap);

		// setStyleName(style.toolbarButton2());
		sinkEvents(Event.ONCLICK);
		setAllowTextSelection(false);
	}

	public TextToolbarButton(String text) {
		this();
		setText(text);
	}

	public void setText(String text) {
		textWrap.setInnerText(text);
	}

	public void setActive(boolean active) {
		this.active = active;
		if (active) {
			buttonWrap.setClassName(style.toolbarButton2Active());
		}
		else {
			buttonWrap.setClassName(style.toolbarButton2());
		}
	}

	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return addDomHandler(handler, ClickEvent.getType());
	}
}